#include "pidsboard.h"

PidsBoadManager::PidsBoadManager( int key, int iPidsCount )
{
	m_iPidsCount = iPidsCount;
	m_ptShmManager = new ShmManager(key);
}

PidsBoadManager::~PidsBoadManager()
{
	delete m_ptShmManager;
}

int PidsBoadManager::Init()
{
	int ret = -1;
	ret = m_ptShmManager->Creat( sizeof(PidBoard) * m_iPidsCount );		
	if ( 0 == ret )
	{
		PidBoard * ptPidBoad = (PidBoard *) m_ptShmManager->GetShm();
		for (int i=0; i<m_iPidsCount; ++i)
		{
			ptPidBoad[i].pid = -1;
			ptPidBoad[i].state = eEmpty;
		}
	}
	return ret;
}

int PidsBoadManager::Destory()
{
	m_ptShmManager->ShmDT();
	return m_ptShmManager->Remove();
}

int PidsBoadManager::New(int pid)
{
	int ret = -1;
	PidBoard * ptPidBoad = (PidBoard *) m_ptShmManager->GetShm();
	for (int i=0; i<m_iPidsCount; ++i)
	{
		if (ptPidBoad[i].state == eEmpty)
		{
			ptPidBoad[i].pid = pid;
			ptPidBoad[i].state = eEmpty;
			ret = 0;
			break;
		}
	}
	return ret;

}

int PidsBoadManager::SetState( int pid, eProcState eState )
{
	int ret = -1;
	PidBoard * ptPidBoad = (PidBoard *) m_ptShmManager->GetShm();

	for (int i=0; i<m_iPidsCount; i++)
	{
		if (ptPidBoad[i].pid == pid)
		{
			ret = 0;
			ptPidBoad[i].state = eState;
			break;
		}
	}
	return ret;
}

bool PidsBoadManager::CheckState(eProcState eState, int &pid)
{
	bool flag = false;
	PidBoard * ptPidBoad = (PidBoard *) m_ptShmManager->GetShm();
	for (int i=0; i<m_iPidsCount; i++)
	{
		if (ptPidBoad[i].state == eState)
		{
			pid = ptPidBoad[i].pid;
			flag = true;
			break;
		}
	}
	return flag;
}



